/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.sec.content.user;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.User;
import han.bifang.gwt.client.common.entity.Usergroup;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.sec.SecurityContentConstants;
import han.bifang.gwt.client.sec.SecurityMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class UserDetailContent extends Content {
	private SecurityMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private String id;

	private VerticalPanel panel;

	private User u;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList a = (ArrayList) res.getResponse();
			u = (User) a.get(0);
			ArrayList<Usergroup> g = (ArrayList<Usergroup>) a.get(1);

			TabPanel tabs = new TabPanel();
			tabs.setWidth("100%");
			UserBasicContent b = new UserBasicContent(cons, service, context, u);
			UserGroupRelContent gr = new UserGroupRelContent(cons, service,
					context, id, g);
			tabs.add(b, cons.basic());
			tabs.add(gr, cons.group());
			tabs.selectTab(0);
			panel.add(tabs);

			HorizontalPanel h = new HorizontalPanel();
			Button remove = new Button(cons.remove());
			remove.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					HashMap p = new HashMap();
					p.put("user", u);
					listener.onContentChanged(
							SecurityContentConstants.USER_DETAIL,
							SecurityContentConstants.REMOVE_USER, p);
				}

			});

			Button ret = new Button(cons.ret());
			ret.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					listener.onContentChanged(
							SecurityContentConstants.USER_DETAIL,
							SecurityContentConstants.SEARCH_USER_RESULT, null);

				}

			});
			if (PrivilegChecker.hasPrivilege(context, null))
				h.add(remove);
			h.add(new HTML("&nbsp;&nbsp;"));
			h.add(ret);
			panel.add(new HTML("&nbsp;"));
			panel.add(h);
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public UserDetailContent(SecurityMessages cons,
			BifangGWTServiceAsync service, GwtContext context, String id) {
		this.service = service;
		this.context = context;
		this.cons = cons;
		this.id = id;
		panel = new VerticalPanel();
		panel.setWidth("100%");
		initWidget(panel);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.sec.user.GetUserDetailAction");
		req.setRequest(id);
		service.call(req, cb);

	}

}
